using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Collections.Generic;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.IO;


namespace TN5Y
{
    public class Utilities
    {

        #region Connection strings

        public static string DBConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["TeamTime"].ConnectionString;
            }
        }
        #endregion

        #region strCat Overloads

        public static string strCat(string sql)
        {
            return strCat(sql, ",", false);
        }

        public static string strCat(string sql, bool quoteEnclose)
        {
            return strCat(sql, ",", quoteEnclose);
        }

        public static string strCat(string sql, string c)
        {
            return strCat(sql, c, false);
        }

        public static string strCat(string sql, string c, bool quoteEnclose)
        {
            string s = "";
            SqlDataReader dr = SqlHelper.ExecuteReader(DBConnectionString, CommandType.Text, sql);
            while (dr.Read())
            {
                s += (quoteEnclose ? "'" : "") + dr[0].ToString() + (quoteEnclose ? "'" : "") + c;
            }
            dr.Close();
            if (s.Length > 0) s = s.Substring(0, s.Length - c.Length);
            return s;
        }

        #endregion

        internal static string isNull(object input, string retValIfNull)
        {
            if (input is DBNull)
            {
                return retValIfNull;
            }
            else
            {
                return input.ToString();
            }
        }

        internal static bool isDate(string str)
        {
            try
            {
                DateTime.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string getConfigAppSettingsValue(string configFilePath, string key)
        {
            string ret = "";
            System.Configuration.Configuration rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(configFilePath);
            if (rootWebConfig1.AppSettings.Settings.Count > 0)
            {
                System.Configuration.KeyValueConfigurationElement customSetting = rootWebConfig1.AppSettings.Settings[key];
                if (customSetting != null)
                    ret = customSetting.Value;
            }
            return ret;
        }


        public static object iif(bool cond, object trueval, object falseval)
        {
            if (cond)
            { return trueval; }
            else
            { return falseval; }
        }

        //public static string generateReportURL(string reportName, string paramString, bool toPDF, bool toZip)
        //{
        //    string url;
        //    if (toPDF)
        //    {
        //        url = "ReportView.aspx?ReportName=" + reportName;
        //        url+=paramString;
        //        if (toZip) { url += "&tozip=true"; }
        //    }
        //    else
        //    {
        //        url = "http://" + ConfigurationManager.AppSettings["ReportingServerName"] + "/reportserver/Pages/ReportViewer.aspx?/TeamTimeReporting/" + reportName + "&rs:Command=Render";
        //        url += paramString;
        //    }
        //    return url;

        //}

        public static string generateReportURL(string reportName, string paramString, bool toPDF, bool toZip, string serverName)
        {
            string url;
            if (toPDF)
            {
                url = "ReportView.aspx?ReportName=" + reportName;
                //url = "DeclarationToPDF.aspx?ReportName=" + reportName;
                url += paramString;
                if (toZip) { url += "&tozip=true"; }
            }
            else
            {
                //Note - we have to use serverName so that external users (tcsptn.dyndns.org) also work
                //url = "http://" + ConfigurationManager.AppSettings["ReportingServerName"] + "/reportserver/Pages/ReportViewer.aspx?/TCMSReporting/" + reportName + "&rs:Command=Render";
                url = getReportServerAddress(serverName) + "/reportserver/Pages/ReportViewer.aspx?/TeamTimeReporting/" + reportName + "&rs:Command=Render";
                url += paramString;
            }
            return url;

        }
        public static string getReportServerAddress(string serverName)
        {
            if (serverName == "") serverName = "localhost";
            return "http://" + serverName + ":8080";
        }

        public static int WriteFile(string fullFilePathWithExt, byte[] byteResultStream)
        {
            int intResult = 0;
            FileStream stream = File.OpenWrite(@fullFilePathWithExt);
            stream.Write(byteResultStream, 0, byteResultStream.Length);
            stream.Close();
            intResult = 1;
            return intResult;
        }

        internal static string getAdminEmail()
        {
            return ConfigurationManager.AppSettings["AdminEmail"];
        }
    }
}



